﻿using L.UI.Base;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PartPanel : BasePanel
{
    public List<Sprite> weatherSprit = new List<Sprite>();

    public Sprite sceneSelect;
    public Sprite sceneNoSelect;

    public Sprite green;
    public Sprite yellow;
    public Sprite red;

    private string weatherReBtnStr = "clearday";
    private int weatherReBtnNum = 0;

    private string sceneReBtnStr = "forest";
    private int sceneReBtnNum = 14;

    private string weatherIntensity = "middle";

    private void Start()
    {
        AddOnClickListener("clearday", SelectDay, 0);
        AddOnClickListener("cloudyday", SelectDay, 1);
        AddOnClickListener("rainyday", SelectDay, 2);
        AddOnClickListener("snowyday", SelectDay, 3);
        AddOnClickListener("fogday", SelectDay, 4);

        AddOnClickListener("plain", SelectScene, 10);
        AddOnClickListener("peak", SelectScene, 11);
        AddOnClickListener("desert", SelectScene, 12);
        AddOnClickListener("gobi", SelectScene, 13);
        AddOnClickListener("forest", SelectScene, 14);
        AddOnClickListener("sea", SelectScene, 15);

        AddOnClickListener("low", SelectDayIntensity);
        AddOnClickListener("middle", SelectDayIntensity);
        AddOnClickListener("height", SelectDayIntensity);

        AddOnValueChangeSlider("Slider", ChangedHandlerColor);
    }

    /// <summary>
    /// 改变滑块颜色
    /// </summary>
    /// <param name="arg0"></param>
    private void ChangedHandlerColor(float arg0)
    {
        if (arg0 < 0.333f)
        {
            GetImage("Handle").sprite = green;
        }
        else if (arg0 < 0.666f)
        {
            GetImage("Handle").sprite = yellow;
        }
        else
        {
            GetImage("Handle").sprite = red;
        }
        ActionManager.Instance.SliderTime?.Invoke(arg0);
    }

    /// <summary>
    /// 选择天气强度
    /// </summary>
    /// <param name="btmName">天气名字</param>
    private void SelectDayIntensity(string btmName)
    {
        GetGameObject(weatherIntensity).GetComponent<Image>().enabled = false;
        GetGameObject(btmName).GetComponent<Image>().enabled = true;
        weatherIntensity = btmName;

        ActionManager.Instance.SelectWeatherDay?.Invoke(btmName, weatherReBtnNum);
    }

    /// <summary>
    /// 选择天气
    /// </summary>
    /// <param name="index">天气序号</param>
    /// <param name="btmName">天气名字</param>

    private void SelectDay(int index, string btmName)
    {
        GetImage(weatherReBtnStr).sprite = weatherSprit[weatherReBtnNum];
        GetGameObject(weatherReBtnStr).transform.GetChild(0).gameObject.SetActive(false);
        GetGameObject(weatherReBtnStr).transform.GetChild(1).GetComponent<Text>().color = new Color(1, 1, 1, 1);

        GetImage(btmName).sprite = weatherSprit[index + 5];
        GetGameObject(btmName).transform.GetChild(0).gameObject.SetActive(true);
        GetGameObject(btmName).transform.GetChild(1).GetComponent<Text>().color = new Color(0, 1, 1, 1);
        weatherReBtnStr = btmName;
        weatherReBtnNum = index;

        SelectDayIntensity("middle");
    }

    /// <summary>
    /// 选择场景
    /// </summary>
    /// <param name="index">场景序号</param>
    /// <param name="btmName">场景名字</param>
    private void SelectScene(int index, string btmName)
    {
        GetImage(sceneReBtnStr).sprite = weatherSprit[sceneReBtnNum];
        GetGameObject(sceneReBtnStr).transform.GetChild(0).GetComponent<Image>().sprite = sceneNoSelect;
        GetImage(btmName).sprite = weatherSprit[index + 6];
        GetGameObject(btmName).transform.GetChild(0).GetComponent<Image>().sprite = sceneSelect;
        sceneReBtnStr = btmName;
        sceneReBtnNum = index;
        ActionManager.Instance.SelectScene?.Invoke(btmName);
    }

    public override void InitUI()
    {
        SelectDay(0, "clearday");
        SelectScene(14, "forest");
    }
}
